import json

def convert_txt_to_json(input_file, output_file):
    records = []
    
    with open(input_file, 'r', encoding='utf-8') as f:
        # 跳过第一行（空行）
        next(f)
        # 跳过表头
        next(f)
        
        # 读取每一行数据
        for line in f:
            # 跳过空行
            if not line.strip():
                continue
                
            # 分割字段
            fields = line.strip().split('\t')
            if len(fields) >= 7:
                record = {
                    "id": int(fields[0]),
                    "mz": fields[1],
                    "dz": fields[2],
                    "zy": fields[3],
                    "bx": fields[4],
                    "xsid": int(fields[5]),
                    "jpm": int(fields[6])
                }
                records.append(record)
    
    # 创建最终的JSON结构
    json_data = {
        "records": records
    }
    
    # 写入JSON文件
    with open(output_file, 'w', encoding='utf-8') as f:
        json.dump(json_data, f, ensure_ascii=False, indent=2)

if __name__ == "__main__":
    input_file = "jdb.txt"
    output_file = "output.json"
    convert_txt_to_json(input_file, output_file)
    print(f"转换完成！数据已保存到 {output_file}")